package com.hhkj.study.api.user.common.handler;

import cn.hutool.core.util.ObjectUtil;
import com.hhkj.captcha.common.handler.CaptchaHandler;
import com.hhkj.captcha.model.extend.Captcha;
import com.hhkj.sms.common.template.AliDaYuTemplate;
import com.hhkj.sms.model.CaptchaTemplateParam;
import com.hhkj.sms.model.base.BaseSendParam;
import com.hhkj.study.admin.user.entity.User;
import com.hhkj.study.admin.user.service.base.UserService;
import com.hhkj.study.admin.user.service.extend.UserExtendService;
import com.hhkj.study.admin.user.service.extend.impl.UserExtendServiceImpl;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 验证码登录
 * @Author luowu
 * @Date 2024/6/28 涵 14:12:39
 */
@Component
public class AccountCaptchaUpdatePwdHandler implements CaptchaHandler {

    @Resource
    private AliDaYuTemplate template;

    @Resource
    private UserExtendService userExtendService;

    @Override
    public String getCode() {
        return "updatePwdSource";
    }

    @Override
    public void beforeSend(String mobile) {
        User user = userExtendService.getByMobileOrIdCard(mobile, null);
        if (ObjectUtil.isNull(user)) {
            throw new RuntimeException("该手机号绑定的用户不存在");
        }
    }

    @Override
    public BaseSendParam createCaptchaSmsBody(Captcha captcha) {
        return template.createSmsBody(new CaptchaTemplateParam(captcha.getCaptcha()));
    }
}
